<template>
  <div >
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-select
          v-model="query.productCode"
          clearable
          filterable
          placeholder="产品编号"
          style="width: 165px;"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in products"
            :key="item.id"
            :label="item.productName"
            :value="item.productCode"
          >
            <span style="float: left">{{ item.productName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.productCode }}</span>
          </el-option>
        </el-select>
        <el-select v-model="query.operationCode" clearable filterable
                   placeholder="工序编号" style="width: 165px;" class="filter-item" @change="crud.toQuery" >
          <el-option
            v-for="item in operationList"
            :key="item.id"
            :label="item.operationName"
            :value="item.operationCode" >
            <span style="float: left">{{ item.operationName }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.operationCode }}</span>
          </el-option>
        </el-select>
        <el-input v-model="query.programOn" clearable placeholder="程序号" style="width: 100px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="产品编号" prop="productCode">
            <el-select @change="productCodeChange" v-model="form.productCode" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in products"
                :key="item.id"
                :label="item.productName"
                :value="item.productCode" >
                <span style="float: left">{{ item.productName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.productCode }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="工序编号" prop="operationCode">
            <el-select @change="operationCodeChange" v-model="form.operationCode" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in operations"
                :key="item.id"
                :label="item.operationName"
                :value="item.operationCode" >
                <span style="float: left">{{ item.operationName }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.operationCode }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="程序号" prop="programOn">
            <el-select v-model="form.programOn" style="width: 370px" filterable placeholder="请选择">
              <el-option
                v-for="item in programs"
                :key="item.id"
                :label="item.programNo"
                :value="item.programNo" >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="描述">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="productCode" label="产品编号" width="150px;" align="center"/>
        <el-table-column label="产品名称" width="150px;" align="center">
          <template slot-scope="scope">
            {{ getProductName(scope.row.productCode) }}
          </template>
        </el-table-column>
        <el-table-column prop="operationCode" label="工序编号" width="150px;" align="center"/>
        <el-table-column prop="programOn" label="程序号" width="80px;" align="center"/>
        <el-table-column prop="remark" label="描述" width="250px;" align="center"/>
        <el-table-column v-if="checkPer(['admin','craftProductProgram:edit','craftProductProgram:del'])" fixed="right" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudCraftProductProgram from '@/api/craft/craftProductProgram'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import crudProductModel from '@/api/product/productModel'
import crudCraftOperationProduct from '@/api/craft/craftOperationProduct'
import crudCraftProgram from '@/api/craft/craftProgram'
import crudCraftOperation from '@/api/craft/craftOperation'

const defaultForm = { id: null, productCode: null, operationCode: null, programOn: null, remark: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'CraftProductProgram',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '程序号', url: 'api/craftProductProgram', idField: 'id', sort: 'id,desc', crudMethod: { ...crudCraftProductProgram }})
  },
  data() {
    return {
      operations: [],
      operationList: [],
      products: [],
      programs: [],
      permission: {
        add: ['admin', 'craftProductProgram:add'],
        edit: ['admin', 'craftProductProgram:edit'],
        del: ['admin', 'craftProductProgram:del']
      },
      rules: {
        operationCode: [
          {required: true, message: '工序编码不能为空', trigger: 'blur'}
        ],
        programNo: [
          {required: true, message: '程序号不能为空', trigger: 'blur'}
        ],
        productCode: [
          {required: true, message: '产品编码不能为空', trigger: 'blur'}
        ]
      },
      queryTypeOptions: [
        { key: 'id', display_name: 'id' },
        { key: 'productCode', display_name: '产品编号' },
        { key: 'operationCode', display_name: '工序编号' },
        { key: 'programOn', display_name: '程序号' },
        { key: 'remark', display_name: '描述' },
        { key: 'createBy', display_name: '创建人' },
        { key: 'createTime', display_name: '创建时间' },
        { key: 'updateBy', display_name: '更新人' },
        { key: 'updateTime', display_name: '更新时间' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    productCodeChange(value){
      crudCraftOperationProduct.getByProduct({productCode: value}).then(res =>{
        if(res){
          this.operations = res;
        }else {
          this.operations = [];
        }
      });
    },
    operationCodeChange(value){
      crudCraftProgram.list({operationCode : value }).then(res =>{
        if(res){
          this.programs = res;
        } else {
          this.programs = [];
        }
      })
    },
    getProductName(value){
      let name = '';
      this.products.forEach(res =>{
        if(res.productCode == value){
          name = res.productName;
          return;
        }
      });
      return name;
    }
  },
  mounted() {
    crudProductModel.list().then(res =>{
      this.products = res;
    });

    //加载工序
    crudCraftOperation.list().then(res => {
      this.operationList = res;
    })
  }
}
</script>

<style scoped>

</style>
